package com.sunday.aomicaichang.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.adpter.SelectAreaChildAdapter;
import com.sunday.aomicaichang.adpter.SelectAreaParentAdapter;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.aomicaichang.model.AreaType;
import com.sunday.aomicaichang.model.CityBean;
import com.sunday.aomicaichang.model.Village;
import com.sunday.aomicaichang.ui.widgets.SidebarView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class SelectAreaActivity extends BaseActivity {


    @Bind(R.id.product_category)
    ListView productCategory;
    @Bind(R.id.product_category_list)
    RecyclerView productCategoryList;
//    @Bind(R.id.indexBar)
//    IndexBar indexBar;
    private List<Village> parentList = new ArrayList<>();
    //    private List<Area.ChildrenBean> childList = new ArrayList<>();
    private List<AreaType> areaTypeList = new ArrayList<>();
    private SelectAreaParentAdapter parentAdapter;
    private SelectAreaChildAdapter childAdapter;
    private int parentId;
    /**
     * 右侧边栏导航区域
     */

    /**
     * 显示指示器DialogText
     */
    private TextView mTvSideBarHint;

    private LinearLayoutManager layoutManager;
    private List<CityBean> mDatas = new ArrayList<>();
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_area);
        ButterKnife.bind(this);
        initAdapters();
        getParentCategory();
    }

    private void initAdapters() {
        parentAdapter = new SelectAreaParentAdapter(mContext, parentList);
        productCategory.setAdapter(parentAdapter);
        layoutManager = new LinearLayoutManager(mContext);
        productCategoryList.setLayoutManager(layoutManager);
//        childAdapter = new SelectAreaChildAdapter(mContext, childList);
        SidebarView sidebarView= (SidebarView) findViewById(R.id.activity_select_sidebar_sv);
        sidebarView.setListView(productCategoryList);
        childAdapter = new SelectAreaChildAdapter(mContext,SelectAreaActivity.this, areaTypeList,sidebarView);
        productCategoryList.setAdapter(childAdapter);
        productCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                parentAdapter.setSelectPosition(position);
                parentId = parentList.get(position).getId();
                if (parentList.get(position).getChildren() != null) {
                    areaTypeList.clear();

                    for (int i = 0; i < parentList.size(); i++) {
                        if (parentId == parentList.get(i).getId()) {
                            List<Village.ChildrenBeanX> childrenBeanX = parentList.get(position).getChildren();
                            if (childrenBeanX != null && childrenBeanX.size() != 0) {
                                areaTypeList.clear();
                                if (mDatas != null)
                                    mDatas.clear();

                                    for (int j = 0; j < childrenBeanX.size(); j++) {
                                        CityBean bean=new CityBean();
                                        bean.setCity(childrenBeanX.get(j).getPyszm());
                                        mDatas.add(bean);
                                        AreaType areaType = new AreaType();
                                        areaType.setAreaType(1);
                                        areaType.setTit(childrenBeanX.get(j).getPyszm());
                                        areaTypeList.add(areaType);
                                        List<Village.ChildrenBeanX.ChildrenBean> childrenBeanList=childrenBeanX.get(j).getChildren();
                                        for (Village.ChildrenBeanX.ChildrenBean children : childrenBeanList) {
                                            AreaType areaType1 = new AreaType();
                                            areaType1.setChildrenBean(children);
                                            children.setParentName(childrenBeanX.get(j).getName());
                                            children.setParentNames(parentList.get(i).getName());
                                            areaTypeList.add(areaType1);
                                        }
                                    }
//                                }
                            }
                        }
                    }
                }
                parentAdapter.notifyDataSetChanged();
//                childAdapter.notifyDataSetChanged();
                childAdapter.setData(areaTypeList);


            }
        });

    }


    private void getParentCategory() {
        Call<ResultDO<List<Village>>> call = ApiClient.getApiAdapter().getVillageByTree();
        call.enqueue(new Callback<ResultDO<List<Village>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Village>>> call, Response<ResultDO<List<Village>>> response) {
                if (isFinish) {
                    return;
                }
                if (response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    parentList.clear();
                    parentList.addAll(response.body().getResult());
                    parentAdapter.notifyDataSetChanged();
                    if (parentList != null && parentList.size() > 0)
                        if (parentId != 0) {
                            for (int i = 0; i < parentList.size(); i++) {
                                if (parentId == parentList.get(i).getId()) {
                                    List<Village.ChildrenBeanX> childrenBeanX=parentList.get(i).getChildren();
                                    if ( childrenBeanX!= null&&childrenBeanX.size()!=0) {
                                        areaTypeList.clear();
                                        if(mDatas!=null)
                                            mDatas.clear();
                                            for(int j=0;j<childrenBeanX.size();j++){
                                                CityBean bean=new CityBean();
                                                bean.setCity(childrenBeanX.get(j).getPyszm());
                                                mDatas.add(bean);
                                                AreaType areaType = new AreaType();
                                                areaType.setAreaType(1);
                                                areaType.setTit(childrenBeanX.get(j).getPyszm());
                                                areaTypeList.add(areaType);
                                                List<Village.ChildrenBeanX.ChildrenBean> childrenBeanList=childrenBeanX.get(j).getChildren();
                                                for (Village.ChildrenBeanX.ChildrenBean children : childrenBeanList) {
                                                    AreaType areaType1 = new AreaType();
                                                    areaType1.setChildrenBean(children);
                                                    children.setParentName(childrenBeanX.get(j).getName());
                                                    children.setParentNames(parentList.get(i).getName());
                                                    areaTypeList.add(areaType1);
                                                }
                                            }

                                        }
                                        parentAdapter.setSelectPosition(i);
                                        break;
                                    }
//                                }
                            }
                        } else {
                            parentId = parentList.get(0).getId();
                            if (parentList.get(0).getChildren() != null) {
                                areaTypeList.clear();

                                for (int i = 0; i < parentList.size(); i++) {
                                    if (parentId == parentList.get(i).getId()) {
                                        List<Village.ChildrenBeanX> childrenBeanX = parentList.get(0).getChildren();
                                        if (childrenBeanX != null && childrenBeanX.size() != 0) {
                                            areaTypeList.clear();
                                            if (mDatas != null)
                                                mDatas.clear();
                                                for (int j = 0; j < childrenBeanX.size(); j++) {
                                                    CityBean bean=new CityBean();
                                                    bean.setCity(childrenBeanX.get(j).getPyszm());
                                                    mDatas.add(bean);
                                                    AreaType areaType = new AreaType();
                                                    areaType.setAreaType(1);
                                                    areaType.setTit(childrenBeanX.get(j).getPyszm());
                                                    areaTypeList.add(areaType);
                                                    List<Village.ChildrenBeanX.ChildrenBean> childrenBeanList=childrenBeanX.get(j).getChildren();
                                                    for (Village.ChildrenBeanX.ChildrenBean children : childrenBeanList) {
                                                        AreaType areaType1 = new AreaType();
                                                        areaType1.setAreaType(2);
                                                        children.setParentName(childrenBeanX.get(j).getName());
                                                        children.setParentNames(parentList.get(i).getName());
                                                        areaType1.setChildrenBean(children);
                                                        areaTypeList.add(areaType1);
                                                    }
                                                }

                                            }
                                            parentAdapter.setSelectPosition(i);
                                            break;
//                                        }
                                    }
                                }
                            }
                        }
                    parentAdapter.notifyDataSetChanged();
//                    childAdapter.notifyDataSetChanged();
                    childAdapter.setData(areaTypeList);
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Village>>> call, Throwable t) {

            }
        });
    }

}

